<?php
class AuthPlugin extends Zend_Controller_Plugin_Abstract
{
	public $_actionController; 
	public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
    	$auth = Zend_Auth::getInstance();
    	
    	if ( !$auth->hasIdentity() ) 
    	{
    		if ( $this->getRequest()->getControllerName() == 'users' &&  ($this->getRequest()->getActionName() == 'login' ||$this->getRequest()->getActionName() == 'register')) return; 
    		$redirector = Zend_Controller_Action_HelperBroker::getHelper('redirector');
    		$redirector->gotoUrl('users/login');
    	}elseif ( $auth->hasIdentity() && $auth->getStorage()->read()->Role != 'immortal' ) 
    	{
    		if ( $this->getRequest()->getControllerName() == 'users' &&  
    		($this->getRequest()->getActionName() == 'index' || $this->getRequest()->getActionName() == 'add')||
    		$this->getRequest()->getActionName() == 'edit')
    		{ 
    			$redirector = Zend_Controller_Action_HelperBroker::getHelper('redirector');
    			$redirector->gotoUrl('');
    		}
    	}
    	
    	if ( $auth->hasIdentity() && ( $auth->getStorage()->read()->Approved == 1 || $auth->getStorage()->read()->Bann == 0)) 
    	{
    		if ( $this->getRequest()->getControllerName() == 'users' &&  $this->getRequest()->getActionName() == 'login') return; 
    		$redirector = Zend_Controller_Action_HelperBroker::getHelper('redirector');
    		$redirector->gotoUrl('users/login?err=1');
    	}
    	
    }
    
}